package com.example.pest.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pest.mapper.ArticleMapper;
import com.example.pest.pojo.Article;
import com.example.pest.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class ArticleController {
    /*
    获取所以文章信息
    * */
    @Autowired
    ArticleMapper  articleMapper;
    @GetMapping("/getAllArticle")
    public List<Article> getAllArticle(){
        System.out.println("getAllArticle");
        List<Article> articles = articleMapper.selectList(null);
        System.out.println(articles);
        return articles;
    }

    @PostMapping("/addArticle")
    public String addArticle(@RequestBody Article article){

        Article article1 = new Article();


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String currentTime = sdf.format(date);
        System.out.println("addArticle");

        article1.setTitle(article.getTitle());
        article1.setContent(article.getContent());
        article1.setUsername(article.getUsername());

        article1.setCreateTime(currentTime);
        System.out.println(article1);
        articleMapper.insert(article1);
        return "success";

    }
    @PostMapping("deleteArticle/{username}")
    public String deleteUser(@PathVariable("username") String username) {
        System.out.println(username);
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("username", username);
        articleMapper.delete(queryWrapper);
        return "delete";
    }
}
